﻿
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Microsoft.Extensions.Logging;
using System.Threading.Tasks;

using MassTransit;
using Newtonsoft.Json;
namespace Athena.Observer
{
    public class DefaultConnectReceiveObserver : BaseObserver<DefaultConnectReceiveObserver>, IReceiveObserver
    {

        public DefaultConnectReceiveObserver(ILogger logger) : base(logger)
        {
        }

        /// <summary>
        ///  Called when a message being consumed produced a fault
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="context"></param>
        /// <param name="duration"></param>
        /// <param name="consumerType"></param>
        /// <param name="exception"></param>
        /// <returns></returns>
        public virtual Task ConsumeFault<T>(ConsumeContext<T> context, TimeSpan duration, string consumerType, Exception exception) where T : class
        {
            //log.Error($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")} CONSUME FAULT  - [{consumerType}]");
            //log.Error($"[MESSAGE]:{exception.Message}");
            //log.Error($"[STACKTRACE]:{exception.StackTrace}");
            //log.Error($"[CONTENT]:{JsonConvert.SerializeObject(context.Message)}");
            return Task.CompletedTask;
        }

        /// <summary>
        /// Called when a message has been consumed by a consumer
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="context"></param>
        /// <param name="duration"></param>
        /// <param name="consumerType"></param>
        /// <returns></returns>
        public virtual Task PostConsume<T>(ConsumeContext<T> context, TimeSpan duration, string consumerType) where T : class
        {
            //log.Info($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")} CONSUME SUCCESS  - [{consumerType}]");
            //log.Info($"[CONTENT]:{JsonConvert.SerializeObject(context.Message)}");
            return Task.CompletedTask;
        }

        /// <summary>
        ///  Called when the message has been received and acknowledged on the transport
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public virtual Task PostReceive(ReceiveContext context)
        {

            //log.Info($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")} POST RECEIVE SUCCESS  - [{context.ContentType.Name}]");
            return Task.CompletedTask;
        }

        /// <summary>
        ///  Called when a message has been delivered by the transport is about to be received
        //     by the endpoint
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public virtual Task PreReceive(ReceiveContext context)
        {
            //log.Info($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")} PRE RECEIVE SUCCESS  - [{context.ContentType.Name}]");
            return Task.CompletedTask;
        }

        /// <summary>
        /// Called when the transport receive faults
        /// </summary>
        /// <param name="context"></param>
        /// <param name="exception"></param>
        /// <returns></returns>
        public virtual Task ReceiveFault(ReceiveContext context, Exception exception)
        {
            //    log.Info($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")} RECEIVE FAULT  - [{context.ContentType.Name}]");
            //    log.Error($"[MESSAGE]:{exception.Message}");
            //    log.Error($"[STACKTRACE]:{exception.StackTrace}");
            //    using (StreamReader sr = new StreamReader(context.GetBody(), Encoding.UTF8))
            //    {
            //        var body = await sr.ReadToEndAsync();
            //        log.Error($"[CONTENT]：{body}");
            //        sr.Close();
            //        sr.Dispose();
            //    }
            return Task.CompletedTask;
        }
    }
}
